package com.qf.utils;

import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.*;

public class MD5Utils {
     private static String zifu ="qwertyuiopasdfghjkjlzxcvbnjmQWERTYUIOPASDFGHJKJLZXCVBNJM";

     private static List chars = new ArrayList();

     static {
          char[] charArray=zifu.toCharArray();
          System.err.println(Arrays.toString(charArray));
//          chars = Arrays.asList(charArray);
          for (char c : charArray) {//遍历放到集合中，最终变成一个集合
               chars.add(c);
            }
     }

     public static String getRandomString(int size){
          //char[] chars = zifu.toCharArray();//转成char数组
//          System.err.println(chars);
          Collections.shuffle(chars);//打乱集合，相当于我们生活中的洗牌
          Random random = new Random();
          StringBuffer stringBuffer = new StringBuffer();
          for (int i = 0; i < size; i++) {//根据我们传递的长度来生成指定长度的随机字符
               int index = random.nextInt(chars.size());//获取一个范围在数组长度范围内的数字
               stringBuffer.append( ( chars.get(index)).toString());
          }
          return stringBuffer.toString();
     }

     public  static  String getMd5(String source,String salt,int times){
//          String digest = DigestUtils.md5DigestAsHex((salt+source+salt).getBytes(StandardCharsets.UTF_8);
//          return digest

          if (times ==1) {
               //System.err.println("0");
               return DigestUtils.md5DigestAsHex((salt +source + salt).getBytes(StandardCharsets.UTF_8));
          }else {
               --times;
               //System.err.println("1");
               return getMd5(getMd5(source,salt,1),salt,times);
          }
     }

     public static int sum(int max){
          if (max == 1) {
               return max;
          }
          return max+sum(max-1);
     }
}
